package
{
	import com.happyelements.isolib.IsoTileObject;
	import com.happyelements.isolib.IsoWorld;
	import flash.geom.Point;
	import com.happyelements.data.DataModel;
	import com.happyelements.components.MapScene;

	/**
	 * @author dongpiggyi
	 */
	public class DraggingMapState implements IMapSceneState
	{
		private var _context:MapSceneContext;
		
		private var _deleteFlag:Boolean;
		
		public function DraggingMapState(context:MapSceneContext) 
		{
			_context = context;
			_deleteFlag = true;
		}
		
		public function onMouseDown(mapScene:MapScene, stageX:Number, stageY:Number):void
		{
//			trace("DraggingMapState: " + "down");
			_context.currentX = stageX;
			_context.currentY = stageY;
		}

		public function onMouseMove(mapScene:MapScene, stageX:Number, stageY:Number):void
		{
//			trace("DraggingMapState: " + "move");
			var offsetX:Number = stageX - _context.currentX;
			var offsetY:Number = stageY - _context.currentY;
			
			_context.currentX = stageX;
			_context.currentY = stageY;
			
			mapScene.x += offsetX;
			mapScene.y += offsetY;
			
			_deleteFlag = false;
		}

		public function onMouseUp(mapScene:MapScene, stageX:Number, stageY:Number):void
		{
//			trace("DraggingMapState: " + "up");
			
			if(_deleteFlag)
			{
				var point:Point = mapScene.convertPointToColRow(stageX, stageY);
				var targetTileObject:IsoTileObject = IsoWorld.instance.getTileObjectAt(point.x, point.y);
				
				if(targetTileObject)
				{
					IsoWorld.instance.removeTileObject(targetTileObject);
				}
			}
			
			_deleteFlag = true;
			
			_context.currentState = _context.beforeDraggingState ? _context.beforeDraggingState : _context.draggingMapState; 
		}

		public function onAddingTile(mapScene:MapScene, stageX:Number, stageY:Number, currentDataModel:DataModel = null):void
		{
		}
	}
}
